{% extends "base.html" %}

{%- macro reserved_field(msb, lsb) -%}
<tr class="reserved-field">
    {%- if msb == lsb %}
    <td>[{{msb}}]</td>
    {%- else %}
    <td>[{{msb}}:{{lsb}}]</td>
    {%- endif %}
    <td>-</td>
    <td>-</td>
    <td>-</td>
    <td></td>
    <td>-</td>
    <td></td>
</tr>
{%- endmacro %}

{%- block content -%}
<table border="1">
    <tr>
        <th>Bits</th>
        <th>Identifier</th>
        <th>Access</th>
        <th>Reset</th>
        <th>Decoded</th>
        <th>Name</th>
        <th></th>
    </tr>
    {%- for field in reversed(list(node.fields())) %}
    {%- if loop.first and field.inst.msb < (node.get_property('regwidth') - 1) %}
    {{reserved_field(node.get_property('regwidth') - 1, field.inst.msb + 1)}}
    {%- elif (not loop.first) and field.inst.msb < loop.previtem.inst.lsb - 1 %}
    {{reserved_field(loop.previtem.inst.lsb - 1, field.inst.msb + 1)}}
    {%- endif %}
    <tr id="{{field.inst.inst_name}}">
        {%- if field.inst.msb == field.inst.lsb %}
        <td>[{{field.inst.lsb}}]</td>
        {%- else %}
        <td>[{{field.inst.msb}}:{{field.inst.lsb}}]</td>
        {%- endif %}
        <td>
            {%- if (has_description(field) or has_enum_encoding(field)) -%}
                <a href="#{{field.inst.inst_name}}.desc">{{field.inst.inst_name}}</a>
            {%- else -%}
                {{- field.inst.inst_name -}}
            {%- endif -%}
        </td>
        <td>
            <span title='{{friendly_access(field.get_property("sw"))}}'>{{field.get_property("sw").name}}</span>
            {%- if field.get_property("onread") -%}
                , <span title='{{friendly_access(field.get_property("onread"))}}'>{{ field.get_property("onread").name -}}</span>
            {%- endif -%}
            {%- if field.get_property("onwrite") -%}
                , <span title='{{friendly_access(field.get_property("onwrite"))}}'>{{ field.get_property("onwrite").name -}}</span>
            {%- endif -%}
        </td>
        <td>
            {%- if field.get_property("reset") != None -%}
            {{"0x%x" % field.get_property("reset")}}
            {%- else -%}
            -
            {%- endif %}
        </td>
        <td>
            <input id="_FieldValueTester{{field.inst.inst_name}}" class="field-value-tester"
                type="text" autocomplete="off"
                data-name="{{field.inst.inst_name}}" oninput="onDecodedFieldInput(this)"/>
            {%- if field.get_property("encode") != None %}
            <select id="_FieldValueEnumTester{{field.inst.inst_name}}" class="field-value-enum-tester"
                data-name="{{field.inst.inst_name}}" onchange="onDecodedFieldEnumChange(this)">
            {% for member in field.get_property("encode") %}
                <option value="{{'0x%x' % member.value}}">{{member.name}}</option>
            {% endfor %}
            </select>
            {%- endif %}
        </td>
        <td>{{(field.get_html_name() or "-")|safe}}</td>
        <td>
            <button type="button" id="_RadixButton{{field.inst.inst_name}}" class="radix-button"
                title="Switch radix" aria-label="Switch radix"
                data-name="{{field.inst.inst_name}}" onclick="onRadixSwitch(this)">H</button>
            <a class="headerlink" href="#{{field.inst.inst_name}}" title="Permalink to this row"></a>
        </td>
    </tr>
    {%- if loop.last and field.inst.lsb != 0 %}
    {{reserved_field(field.inst.lsb - 1, 0)}}
    {%- endif %}
    {%- endfor %}
</table>
<p>
    {%- block reg_value_edit %}
    <label>
        Encoded Register Value:
        <input id="_RegValueTester" type="text" autocomplete="off" oninput="onEncodedRegInput(this)"/>
        <button type="button" class="reset-button" title="Reset register values" aria-label="Reset register values" onclick="onResetRegValue(this)"></button>
    </label>
    {%- endblock reg_value_edit %}
</p>

{%- for field in reversed(list(node.fields())) if (has_description(field) or has_enum_encoding(field) or has_extra_property_doc(field)) %}
{%- if loop.first %}
<hr/><h2>Field Descriptions</h2>
{%- endif %}
{% include "field_description.html" %}
{%- endfor %}

{%- endblock content %}
